<?php

use App\Console\Commands\Command\GenerateTrainStationMap;
use App\Console\Commands\Command\Init\ApplicationInit;
use App\Console\Commands\Command\UpdateAreas;
use App\Console\Commands\Command\UpdateTrainStationTranslationBy;
use App\Models\Train\Stations\Repositories\Interfaces\StationRepositoryInterface;
use App\Trip\Train\Order\Impl\SnapTaskLowLevel\SnapTaskLowLevelImpl;
use Illuminate\Support\Facades\Artisan;


/**
 *  初始化系统的区域 - 火车站点等服务
 */
Artisan::command('app:init', function () {
    ApplicationInit::init();
});

/**
 * 翻译 火车站点
 */
Artisan::command('translate:train_station', function () {
    UpdateTrainStationTranslationBy::byExcel();
});

/**
 * 更新行政区域
 */

Artisan::command('area:fetchAll', function () {
    UpdateAreas::fetchAll();
});

/**
 * 低速抢票
 */
Artisan::command('SnapTask:snap {--task_id=} {--action=}', function () {
    $task_id = $this->options()['task_id'] ?? '';
    $action  = $this->options()['action'] ?? '';
    if (!$task_id || !$action) {
        return false;
    }
    return new SnapTaskLowLevelImpl($task_id, $action);
});


/**
 * 火车站匹配行政区域
 */
Artisan::command('area:match:train_station {--force=} {--station=}', function () {
    $force   = $this->options()['force'] ?? '';
    $station = $this->options()['station'] ?? '';
    $map     = new  GenerateTrainStationMap;
    if ($force === "true") {
        $map->setForceMatch();
    }

    if ($station) {
        $stationRepo = app(StationRepositoryInterface::class);
        $info        = $stationRepo->findOneByCode($station) ?: $stationRepo->findOneByName($station);
        $info && $map->setStations(collect([$info]));
    }
    $map->matchStationCity();
});

/**
 * 火车站点查询
 */
Artisan::command('query:station2stations', function () {

    $stationRepo = app(StationRepositoryInterface::class);
    $stations    = $stationRepo->listStations();
    foreach ($stations as $from_station) {
        Artisan::call("station2stations --from_station_code={$from_station->getStationCode()}");
    }
});

